﻿Imports System.Windows.Controls
Imports System.Xml
Imports FuncClasses
Imports HtmlAgilityPack
Imports System.Collections.ObjectModel
Imports System.Windows.Media.Imaging
Imports System.Net
Imports System.Windows.Threading
Imports System.Reflection

Public Class AuftragsTab
    WithEvents _parent As Biller.AuftragRibbon
    Private _auf As Order
    Private currentPackager As IPackager

    Public Sub New(ByRef auftrag As Order, ByRef parent As Biller.AuftragControl, loader As IDatabase)
        _parent = parent.ribbon
        _auf = auftrag
        helper = loader
        InitializeComponent()
        LoadData()
        LoadPackage(auftrag)
    End Sub

    Private Sub AuftragSaved() Handles _parent.QuickSave
        AuftragNichtGespeichert.Visibility = Windows.Visibility.Collapsed
        AuftragStandard.Visibility = Windows.Visibility.Visible
        If ComboBox1.SelectedIndex > -1 Then
            SaveCurrentPackage()
        End If
    End Sub

#Region "XML"
    Private xml As New XmlDocument
    Private helper As IDatabase
    Private Sub InitializeFile(path As String)
        Dim sb As New Text.StringBuilder
        sb.Append(path)
        'if the file doesn't exist, create a blank xml
        If Not IO.File.Exists(path) Then
            Dim fn As New IO.StreamWriter(IO.File.Open(path, IO.FileMode.Create))
            fn.WriteLine("<?xml version=""1.0"" encoding=""utf-8""?>")
            fn.WriteLine("<trackinglist>")
            fn.WriteLine("</trackinglist>")
            fn.Close() 'all done
        End If
    End Sub

    Private Sub LoadData()
        InitializeFile(System.AppDomain.CurrentDomain.BaseDirectory() + "Firma\" + helper.LastCompany + "\tracking.xml")
        xml.Load(System.AppDomain.CurrentDomain.BaseDirectory() + "Firma\" + helper.LastCompany + "\tracking.xml")
    End Sub

    Private Sub LoadPackage(ByVal auf As Order)
        Dim xpath As String = "/trackinglist/package[@auftragsnr=""{0}"" and @auftragsart=""{1}""]"
        Dim int As Integer = auf.DokumentArt
        xpath = String.Format(xpath, auf.OrderID.ToString, int.ToString)
        Dim root As XmlElement = xml.DocumentElement.SelectSingleNode(xpath)

        If root IsNot Nothing Then
            TextBox2.Text = root.Attributes.GetNamedItem("trackingnr").Value.ToString
            Dim packa As Packager = root.Attributes.GetNamedItem("packager").Value
            ComboBox1.SelectedIndex = packa
        End If
    End Sub

    Private Sub SaveCurrentPackage()
        CreateXMLNode(TextBox2.Text, ComboBox1.SelectedIndex, _auf)
        xml.Save(System.AppDomain.CurrentDomain.BaseDirectory() + "Firma\" + helper.LastCompany + "\tracking.xml")
    End Sub

    Private Sub CreateXMLNode(ByVal trackingnr As String, ByVal packager As Packager, ByVal auf As Order)
        Dim xpath As String = "/trackinglist/package[@auftragsnr=""{0}"" and @auftragsart=""{1}""]"
        Dim int As Integer = auf.DokumentArt
        xpath = String.Format(xpath, auf.OrderID.ToString, int.ToString)
        Dim root As XmlElement = xml.DocumentElement.SelectSingleNode(xpath)
        If root IsNot Nothing Then
            root.Attributes.GetNamedItem("date").Value = auf.Datum.ToString
            root.Attributes.GetNamedItem("packager").Value = ComboBox1.SelectedIndex.ToString
            root.Attributes.GetNamedItem("trackingnr").Value = TextBox2.Text
        Else
            root = xml.DocumentElement.SelectSingleNode("/trackinglist")
            Dim temp As XmlElement = xml.CreateElement("package")
            temp.SetAttribute("trackingnr", trackingnr)
            temp.SetAttribute("packager", packager)
            temp.SetAttribute("auftragsnr", auf.OrderID)
            temp.SetAttribute("auftragsart", auf.DokumentArt)
            temp.SetAttribute("date", auf.Datum.ToString)
            root.AppendChild(temp)
        End If
    End Sub
#End Region
    
    Private Sub Button3_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button3.Click
        If ComboBox1.SelectedIndex > -1 Then
            SaveCurrentPackage()
            currentPackager.Package_ID = TextBox2.Text
        End If
    End Sub

    Private Sub ComboBox1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
        If ComboBox1.SelectedIndex <> -1 Then
            Select Case ComboBox1.SelectedIndex
                Case 0
                    'DHL
                    Dim dhl As New DHL(TextBox2.Text)
                    UIContent.Children.Clear()
                    UIContent.Children.Add(dhl.InfoControl)
                    currentPackager = dhl
                Case 1
                    'DPD
                    Dim dpd As New DPD(TextBox2.Text)
                    UIContent.Children.Clear()
                    UIContent.Children.Add(dpd.InfoControl)
                    currentPackager = dpd
                Case 2
                    Dim h As New Hermes(TextBox2.Text)
                    UIContent.Children.Clear()
                    UIContent.Children.Add(h.InfoControl)
                    currentPackager = h
                Case 5
                    Dim dhlb = CustomPlugIn.DHL_BusinessManager
                    If dhlb Is Nothing Then
                        dhlb = New DHLOnlineLogin(Dispatcher)
                        CustomPlugIn.DHL_BusinessManager = dhlb
                    End If
                    Dim dhl As New DHLBusiness(dhlb, TextBox2.Text)
                    UIContent.Children.Clear()
                    UIContent.Children.Add(dhl.InfoControl)
                    currentPackager = dhl
            End Select
        End If
    End Sub
End Class

Public Enum Packager
    DHL = 0
    DPD = 1
    Hermes = 2
    UPS = 3
    GLS = 4
    DHL_Business = 5
End Enum